package com.hkfg.service.impl;

import com.hkfg.dao.FriendDao;
import com.hkfg.dao.UserDao;
import com.hkfg.domain.Friend;
import com.hkfg.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {

    @Autowired
    private FriendDao friendDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<Friend> selectAllFriends(int id) {
        return friendDao.selectAllFriend(id);
    }

    @Override
    public int deleteFriend(int id, String fname) {
        return friendDao.deleteFriend(id, fname);
    }

    /**
     * 添加两次，双向，因为数据库中user_id是一位，所有存储好友关系用双向关系存储和表示
     * @param userId
     * @param id
     * @return
     */
    @Override
    public int addFriend(int userId, int id) {
        String username = userDao.selectById(userId).getUsername();
        String fname = userDao.selectById(id).getUsername();
        return friendDao.addFriend(username, id) + friendDao.addFriend(fname, userId);
    }
}
